let head = [1, 2, 3, 4, 2, 1];
function ListNode(val, next) {
    this.val = (val === undefined ? 0 : val);
    this.next = (next === undefined ? null : next);
}
let list = new ListNode(head[0]);
let num = list;
// 修正：从索引1开始添加节点（避免重复添加head[0]）
for (let i = 1; i < head.length; i++) {
    num.next = new ListNode(head[i]);
    num = num.next;
}

var isPalindrome = function (head) {
    if (!head) return true;
    let slow = head;
    let fast = head;
    while (fast.next && fast.next.next) {
        slow = slow.next;
        fast = fast.next.next;
    }
    let pre = null;
    let cur = slow.next;
    // 反转后半部分链表
    while (cur) {
        const nxt = cur.next;
        cur.next = pre;
        pre = cur;
        cur = nxt;
    }
    // 比较前半部分和反转后的后半部分
    while (pre) {
        // 修正：比较节点的值而非节点对象
        if (head.val !== pre.val) {
            return false;
        }
        head = head.next;
        pre = pre.next;
    }
    return true;
};
console.log(isPalindrome(list)); // 输出: true